/**
 * @CoypRight:lijq (c) 2016 All Rights Reserved
 * @Title: ScreenHelper.java
 * @Package com.jumi.hotgirl.util
 * @author lijianqun
 * @date 2016-6-22 下午2:22:52
 * @version V1.0
 */
package com.sv.jm.baselibrary.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * @ClassName: ScreenHelper
 * @Description: 计算屏幕宽高
 * @author lijianqun
 * @email lijq@gmail.com‍
 * @date 2016-6-22 下午2:22:52
 */
public class ScreenHelper {
    /** 得到设备屏幕的宽度 **/
    public static int getScreenWidth(Context context) {
        return getDisplayMetrics(context).widthPixels;
    }

    /** 得到设备屏幕的高度 **/
    public static int getScreenHeight(Context context) {
        return getDisplayMetrics(context).heightPixels;
    }

    /** 得到手机分辨率 width x height **/
    public static String getScreenRP(Context context) {
        return String.format("%sx%s", getScreenWidth(context), getScreenHeight(context));
    }

    // 得到设备屏幕
    private static DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }

    public static int sp2px(Context context, float spValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (spValue * scale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将px装换成dp，保证尺寸不变
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dp(Context context, float pxValue) {
        float density = context.getResources().getDisplayMetrics().density;// 得到设备的密度
        return (int) (pxValue / density + 0.5f);
    }

    public static int dp2px(Context context, float dpValue) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * density + 0.5f);
    }
}
